﻿/* ***********************************************
 * Author		:  kingthy
 * Email		:  kingthy@gmail.com
 * Description	:  DbObjectPropertyDescriptor
 *
 * ***********************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace MX.Core.Data
{
    /// <summary>
    /// 数据对象的属性描述
    /// </summary>
    internal sealed class DbObjectPropertyDescriptor 
        : PropertyDescriptor
    {
        private Type _ComponentType;
        private Type _PropertyType;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataName"></param>
        /// <param name="componentType"></param>
        /// <param name="propertyType"></param>
        /// <param name="attrs"></param>
        public DbObjectPropertyDescriptor(string dataName,
            Type componentType,            
            Type propertyType,
            Attribute[] attrs)
            : base(dataName, attrs)
        {
            this._ComponentType = componentType;
            this._PropertyType = propertyType;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="component"></param>
        /// <returns></returns>
        public override bool CanResetValue(object component)
        {
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        public override Type ComponentType
        {
            get { return this._ComponentType; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="component"></param>
        /// <returns></returns>
        public override object GetValue(object component)
        {
            var obj = component as DbObject;
            if (obj != null)
            {
                bool existing;
                var o = obj.GetDataInternal(this.Name, out existing);
                if (existing) return o;
            }
            var pro = component.GetType().GetProperty(this.Name);
            return pro == null ? null : pro.GetValue(component, null);
        }
        /// <summary>
        /// 
        /// </summary>
        public override bool IsReadOnly
        {
            get { return false; }
        }
        /// <summary>
        /// 
        /// </summary>
        public override Type PropertyType
        {
            get { return this._PropertyType; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="component"></param>
        public override void ResetValue(object component)
        {
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="component"></param>
        /// <param name="value"></param>
        public override void SetValue(object component, object value)
        {
            var obj = component as DbObject;
            if (obj != null && obj.ContainData(this.Name))
            {
                obj.SetDataInternal(this.Name, value);
            }
            else
            {
                var pro = component.GetType().GetProperty(this.Name);
                if (pro != null) pro.SetValue(component, value, null);
            }
            this.OnValueChanged(component, EventArgs.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="component"></param>
        /// <returns></returns>
        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }
    }
}
